<!DOCTYPE html>
<html>
<head>
  <th:block th:include="include :: header('编辑分类')" />
  <style>
    body {
      background-color: #ffffff;
    }
  </style>
</head>
<body>
<div class="layui-form layui-form-pane">
  <input id="categoryId" name="categoryId" type="hidden" th:value="${category.id}">
  <div class="layui-form-item">
    <label class="layui-form-label">分类名<span style="color: red">*</span></label>
    <div class="layui-input-block">
      <input type="text" name="categoryName" th:value="${category.categoryName}" lay-verify="required" lay-reqtext="分类名为空" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">描述</label>
    <div class="layui-input-block">
      <input type="text" name="remark" th:value="${category.remark}" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">是否启用</label>
    <div class="layui-input-block">
      <input type="checkbox"  th:attr="checked=(${category.enable} == 1 ? true: false)" name="enable" lay-skin="switch" lay-filter="switch" lay-text="ON|OFF">
    </div>
  </div>

  <div class="layui-form-item" style="margin: 25px 10px;position:absolute;bottom: 0;right: 5%;">
    <div class="layui-input-block">
      <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
      <button type="button" class="layui-btn layui-btn-primary" onclick="javascript:parent.layer.close(parent.layer.getFrameIndex(window.name));">取消</button>
    </div>
  </div>
</div>
<th:block th:include="include :: footer" />
<script>
  layui.use(['form'], function () {
    var form = layui.form;

    //监听提交
    form.on('submit(saveBtn)', function (obj) {
      var data = obj.field;
      var enable = $("[name='enable']").is(':checked');
      enable = enable === true ? 1 : -1;

      const category = {
        id: $("#categoryId").val(),
        categoryName: data.categoryName,
        remark: data.remark,
        enable: enable
      }

      updateCategory(category);
    });

    /**
     * 更新分类
     * @param data
     * @return
     */
    function updateCategory(data){
      $.ajax({
        url: "/category/admin/updateCategory",
        type: "post",
        contentType: "application/json",
        dataType: "json",
        data:JSON.stringify(data),
        success: function (res){
          if (res.state){
            Pt.showMsg("修改成功");
            parent.layer.closeAll();
            parent.window.location.reload()
          }else {
            Pt.showErrorMsg(res.msg);
          }
        },
        error(res){
          console.log(res);
        }
      })
    }


  });
</script>
</body>
</html>